package com.atguigu.process.controller.api;

import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.model.process.Process;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.process.service.OaProcessService;
import com.atguigu.process.service.OaProcessTemplateService;
import com.atguigu.process.service.OaProcessTypeService;
import com.atguigu.vo.process.ApprovalVo;
import com.atguigu.vo.process.ProcessFormVo;
import com.atguigu.vo.process.ProcessVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author dale
 * @title ProcessController
 * @date 2023/6/6 9:09
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping(value="/admin/process")
@CrossOrigin  //跨域,为后续微信公众号准备
public class ProcessController {
        @Autowired
        private OaProcessTypeService processTypeService;

        @Autowired
        private OaProcessTemplateService processTemplateService;

        @Autowired
        private OaProcessService processService;

        @Autowired
        private SysUserService sysUserService;

        @ApiOperation(value = "来到我的界面，获取当前用户基本信息")
        @GetMapping("getCurrentUser")
        public Result getCurrentUser() {
                Map<String,Object>map=sysUserService.getCurrentUser();
                return Result.ok(map);
        }


        @ApiOperation(value = "已发起")
        @GetMapping("/findStarted/{page}/{limit}")
        public Result findStarted(
                @ApiParam(name = "page", value = "当前页码", required = true)
                @PathVariable Long page,

                @ApiParam(name = "limit", value = "每页记录数", required = true)
                @PathVariable Long limit) {
                Page<ProcessVo> pageParam = new Page<>(page, limit);
                IPage<ProcessVo> pageModel=processService.findStarted(pageParam);
                return Result.ok(pageModel);
        }

        @ApiOperation(value = "已处理")
        @GetMapping("/findProcessed/{page}/{limit}")
        public Result findProcessed(
                @ApiParam(name = "page", value = "当前页码", required = true)
                @PathVariable Long page,
                @ApiParam(name = "limit", value = "每页记录数", required = true)
                @PathVariable Long limit) {
                Page<Process> pageParam = new Page<>(page, limit);
                IPage<ProcessVo> pageModel=processService.findProcessed(pageParam);
                return Result.ok(pageModel);
        }

        @ApiOperation(value = "进行审批")
        @PostMapping("approve")
        public Result approve(@RequestBody ApprovalVo approvalVo) {
                processService.approve(approvalVo);
                return Result.ok();
        }

        @ApiOperation(value = "获取审批详情")
        @GetMapping("show/{id}")
        public Result show(@PathVariable Long id) {
                Map<String,Object>map=processService.show(id);
                return Result.ok(map);
        }

        @ApiOperation(value = "待处理列表")
        @GetMapping("/findPending/{page}/{limit}")
        public Result findPending(
                @ApiParam(name = "page", value = "当前页码", required = true)
                @PathVariable Long page,
                @ApiParam(name = "limit", value = "每页记录数", required = true)
                @PathVariable Long limit) {

                Page<ProcessVo> pageParam = new Page<>(page, limit);
                return Result.ok(processService.findPending(pageParam));
        }

        @ApiOperation(value = "启动流程实例")
        @PostMapping("/startUp")
        public Result start(@RequestBody ProcessFormVo processFormVo) {
                processService.startUp(processFormVo);
                return Result.ok();
        }

        @ApiOperation(value = "根据id获取审批模板")
        @GetMapping("getProcessTemplate/{processTemplateId}")
        public Result get(@PathVariable Long processTemplateId) {
                ProcessTemplate processTemplate = processTemplateService.getById(processTemplateId);
                return Result.ok(processTemplate);
        }

        @ApiOperation(value = "获取全部审批分类及模板")
        @GetMapping("findProcessType")
        public Result findProcessType() {
            return Result.ok(processTypeService.findProcessType());
        }

}
